package com.bee.bootstrap.resource.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bee.bootstrap.resource.entity.Resource;
import com.bee.bootstrap.resource.service.IResourceService;
import com.bee.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liekkas
 * @since 2018-05-25
 */
@RestController
@RequestMapping("/resources")
@Api(value = "/resources", description = "资源管理")
public class ResourceController {

    @Autowired
    private IResourceService resourceServiceImpl;

    @GetMapping("")
    @ApiOperation(value="获取资源分页对象", notes="获取资源分页对象")
    public Result findResources(@ModelAttribute("resource") Resource resource, Page<Resource> page){
        return resourceServiceImpl.findPageView(page,resource);
    }

    @GetMapping("/interface")
    @ApiOperation(value="获取资源类型为接口的数据", notes="获取资源类型为接口的数据")
    public Result findResourcesInterface(){
        return resourceServiceImpl.findResourcesInterface();
    }

    @GetMapping("/tree")
    @ApiOperation(value="获取资源分页树形数据", notes="获取资源分页树形数据")
    public Result findResourcesTree(){
        return resourceServiceImpl.selectTreeView();
    }

    @PutMapping("/{id}")
    public Result updateResource(@Valid @RequestBody Resource resource, @PathVariable(required = true,value ="id") long id, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return resourceServiceImpl.updateResource(resource);
    }

    @PostMapping("")
    public Result createResource(@Valid @RequestBody Resource resource,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return resourceServiceImpl.insertResource(resource);
    }



}
